package com.example.tse.rbyair.db;

import java.io.File;


import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Environment;
import android.util.Log;

public class DBHelper extends SQLiteOpenHelper {
	public static final String TAG = "DBHelper";
	private static String path;
	
	static{
		try {
			boolean sdCardExist = Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED); //判断sd卡是否存在 
			if (sdCardExist) {
				path = Environment.getExternalStorageDirectory().toString();//获取跟目录 
			}
			if(path!=null && !path.equals("")){
				path+="/rbyair/";
				File file=new File(path);
				if(!file.exists()){
					file.mkdir();
				}
			}
		} catch (Exception ex) {
			Log.e(TAG, ex.getMessage(),ex);
		}
		
	}
	
	public DBHelper(Context context) {

		super(context, /*path +*/ DBConstants.DATABASE_NAME, null, DBConstants.DATABASE_VERSION);
		
	}
	
	@Override
	public void onCreate(SQLiteDatabase db) {
		Log.v(TAG, "onCreate");
		db.execSQL(DBConstants.CREATE_TB_USER);
		db.execSQL(DBConstants.CREAT_TB_HISTORY);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		if (oldVersion > newVersion) {
			db.beginTransaction();
			db.execSQL("DROP TABLE IF EXISTS " + DBConstants.TB_USER + ";");
			db.setTransactionSuccessful();
			db.endTransaction();
			onCreate(db);
		}
		if (oldVersion == 1) {
			// do something
			oldVersion = 2;
		}
	}

}
